#pragma once
#include <errno.h>
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>

/* POSIX */
#include <unistd.h>
#include <sys/user.h>
#include <sys/wait.h>
#include <syscall.h>

#include <linux/types.h>
#include <sys/ptrace.h>

static bool xx_trace_me()
{
    auto ret = ptrace(PTRACE_TRACEME, 0L, 0L, 0L);
    X_CHECK(ret >=0 ,false);
    return true;
}

static bool xx_trace_attach(pid_t pid){
    auto ret = ptrace(PTRACE_ATTACH, pid, NULL, NULL);
    X_CHECK(ret >=0 ,false);
    return true;
}

static bool xx_trace_get_reg(pid_t child,user_regs_struct &reg)
{
    memset(&reg,0,sizeof(reg));
    auto ret = ptrace(PTRACE_GETREGS, child, NULL, &reg);
    X_CHECK(ret >=0 ,false);
    return true;
}

static string xx_waitstate2str(int status)
{
    if(WIFEXITED(status))       return "terminated normally\n";
    if(WIFSIGNALED(status))     return "terminated by a signal\n";
    if(WIFSTOPPED(status))      return "stopped by delivery of a signal\n";
    if(WIFCONTINUED(status))    return "resumed\n";

    return "state?\n";
}